﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string defaultPrinter;
        private FormWindowState mLastState;        

        public Form1()
        {
            InitializeComponent();
            mLastState = this.WindowState;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fetchDefaultPrinter();
            updatePrinterList();

            
            
        }


        protected override void OnClientSizeChanged(EventArgs e)
        {
            if (this.WindowState != mLastState)
            {
                mLastState = this.WindowState;
                OnWindowStateChanged(e);
            }
            base.OnClientSizeChanged(e);
        }

        protected void OnWindowStateChanged(EventArgs e)
        {
            if (mLastState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                this.ShowInTaskbar = false;
                this.Visible = false;
                notifyIcon1.ContextMenuStrip = contextMenuStrip1;
                notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

            }
            else if (mLastState == FormWindowState.Normal)
            {
                notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
                this.Visible = true;
            }
        }

        

        





        // Printer Functions
        private void fetchDefaultPrinter()
        {
            Printers printers = new Printers();
            defaultPrinter = printers.getDefaultPrinter();

            lbl_currentPrinter.Text = defaultPrinter;

            showDefaultPrinterInMainPrintList();
        }

        private void showDefaultPrinterInMainPrintList()
        {
            for (int i = listBox1.Items.Count - 1; i >= 0; i--)
            {
                string printer = i.ToString();

                if (printer.Equals(defaultPrinter))
                {

                    //int index = listBox1.FindString(printer);
                    //listBox1.SetSelected(index, true);

                    listBox1.SetSelected(2, true);

                }
            }
        }

        private void setPageSetupMargins()
        {
            Printers printers = new Printers();
            printers.setPageSetupMargins("0.11667", "0.11667", "0.11667", "0.11667");
        }



        // Button Events
        private void btnSetasDefault_Click(object sender, EventArgs e)
        {
            Printers printers = new Printers();
            printers.setDefaultPrinter((string)listBox1.SelectedItem);
            fetchDefaultPrinter();
            showDefaultPrinterInMainPrintList();
        }

        private void btnResetMargins_Click(object sender, EventArgs e)
        {
            setPageSetupMargins();
        }


        

        //Context Menu Events
        public void updatePrinterList() //addes printer to contextmenu
        {
            Printers printers = new Printers();
            ToolStripMenuItem Printers = new ToolStripMenuItem("Printers");

            foreach (var print in printers.GetInstalledPrinters())
            {

                listBox1.Items.Add(print);

                ToolStripButton menu_item = new ToolStripButton(print);
                menu_item.Text = print;
                menu_item.Click += new EventHandler(printer_menu_Click);

                menu_item.Width = (print.Length * 4);

                Printers.DropDownItems.Add(menu_item);
            }



            contextMenuStrip1.Items.Add(Printers);

            showDefaultPrinterInMainPrintList();

        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }


        private void printer_menu_Click(object sender, System.EventArgs e) //ContextMent Printers click handler
        {

            ToolStripItem mn = (ToolStripItem)sender;

            Printers printers = new Printers();
            printers.setDefaultPrinter(mn.Text);

            fetchDefaultPrinter();
            setPageSetupMargins();

            notifyIcon1.Text = mn.Text;

        }



    }
}
